package com.business.log;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.log.LogFavorite;
import com.sql.DBFactory;

public class LogFavorBus {
	private static final Log log = LogFactory.getLog(LogFavorBus.class);

	private Connection conn = null;

	private Statement stmt = null;

	private ResultSet rs = null;

	public void add(LogFavorite lf) {
		String favName = lf.getFavName();
		String favInfo = lf.getFavInfo();
		String username = lf.getUsername();
		String logNo = lf.getLogNo();
		String favNo = lf.getFavNo();
		String favAddTime = lf.getFavAddTime();

		String sql = "insert into log_favorite(fav_name,fav_info,fav_no,log_no,fav_addTime,username) values('";
		sql += favName + "','" + favInfo + "','" + favNo + "','" + logNo
				+ "','" + favAddTime + "','" + username + "')";
		try {
			conn = DBFactory.getConnection();

			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the insert logfavorite exception:" + e.getMessage());
			log.error("the insert logfavorite sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public boolean IsFavor(String username, String logNo) {
		String sql = "select * from log_favorite where username='" + username
				+ "' and log_no='" + logNo + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				return true;
			}
			return false;
		} catch (Exception e) {
			log.error("the select logfavorite exception:" + e.getMessage());
			log.error("the select logfavorite sql is: " + sql);
			return false;
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	@SuppressWarnings("unchecked")
	public List getAllFavorsByUserName(String username) {
		List list = new ArrayList();
		String sql = "select * from log_favorite where username='" + username
				+ "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogFavorite lf = new LogFavorite();
				lf.setFavId(rs.getString("fav_id"));
				lf.setFavAddTime(rs.getString("fav_addTime"));
				lf.setFavInfo(rs.getString("fav_info"));
				lf.setFavName(rs.getString("fav_name"));
				lf.setFavNo(rs.getString("fav_no"));
				lf.setLogNo(rs.getString("log_no"));
				list.add(lf);
			}
		} catch (Exception e) {
			log.error("the select logfavorite exception:" + e.getMessage());
			log.error("the select logfavorite sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return list;
	}

	public void del(String FavorNo) {
		String sql = "delete from log_favorite where fav_no='" + FavorNo + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the del logfavorite exception:" + e.getMessage());
			log.error("the del logfavorite sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
	}

	public void update(LogFavorite lf, String favorNo) {
		String favName = lf.getFavName();
		String favInfo = lf.getFavInfo();
		String username = lf.getUsername();
		String logNo = lf.getLogNo();
		String sql = "update log_favorite set  fav_name='" + favName
				+ "',fav_info='" + favInfo + "',log_no='" + logNo
				+ "',username='" + username + "' where fav_no='" + favorNo
				+ "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update logfavorite exception:" + e.getMessage());
			log.error("the update logfavorite sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
	}

	public LogFavorite getFavorByNo(String favorNo) {
		
		LogFavorite lf = new LogFavorite();
		String sql = "select * from log_favorite where fav_no='" + favorNo
				+ "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs=stmt.executeQuery(sql);
			while (rs.next()) {
				lf.setFavId(rs.getString("fav_id"));
				lf.setFavAddTime(rs.getString("fav_addTime"));
				lf.setFavInfo(rs.getString("fav_info"));
				lf.setFavName(rs.getString("fav_name"));
				lf.setFavNo(rs.getString("fav_no"));
				lf.setLogNo(rs.getString("log_no"));
			}
		} catch (Exception e) {
			log.error("the select logfavorite exception:" + e.getMessage());
			log.error("the select logfavorite sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return lf;
	}
}
